<?php
namespace App\Service\Admin;

use Illuminate\Cache\RedisTaggedCache;

class YiyuanService extends BaseService
{
    //医院

    /**
     * 列表
     * @param array $pages【start，limit】
     * @param array $pas
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbYiyuan)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $logo = file_exists($v['logo']) ? env('DOMAIN').'/'.$v['logo'] : '';
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'logo' => $logo,
                'address' => $v['address'] ? $v['address'] : '-',
                'map' => $v['map'],
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 一个
     * @param int $id 门店ID
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbYiyuan)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有商品');
        }
        $arys = [
            'id' => $res['id'],
            'title' => $res['title'],
            'logo' => file_exists($res['logo']) ?  $res['logo'] : '',
            'address' => $res['address'],
            'map' => $res['map'],
            'uptime' => $res['uptime'],
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 添加
     * @param array $pas【title 门店名称，$address 地址，map 坐标】
     */
    public function addOne($pas)
    {
        $resOne = (array) $this->dbConn($this->dbYiyuan)
            ->where('del', 0)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($resOne)) {
            return array('code' => -2, 'msg' => '有重复');
        }
        $sData = [
            'title' => $pas['title'],
            'address' => $pas['address'],
            'logo' => $pas['logo'],
            'map' => $pas['map'],
            'addtime' => toDate(),
            'uptime' => toDate(),
        ];
        $sid = $this->dbConn($this->dbYiyuan)->insertGetId($sData);
        if (!$sid) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code'=>0, 'msg'=>'success');
    }

    /**
     * 修改
     * @param int $id 门店ID
     * @param array $pas【title 门店名称，$address 地址，map 坐标】
     */
    public function modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbYiyuan)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '门店不存在');
        }
        $sData = [
            'title' => $pas['title'],
            'address' => $pas['address'],
            'logo' => $pas['logo'],
            'map' => $pas['map'],
            'uptime' => toDate(),
        ];
        $sup = $this->dbConn($this->dbYiyuan)->where('id', $id)->update($sData);
        if (!$sup) {
            return array('code' => -1, 'msg' => 'error');
        }
        return array('code'=>0, 'msg'=>'success');
    }

    /**
     * 删除
     * @param int $id 门店ID
     */
    public function setDel($id)
    {
        $res = (array) $this->dbConn($this->dbYiyuan)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '门店不存在');
        }
        $arys = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = $this->dbConn($this->dbYiyuan)->where('id', $id)->update($arys);
        if (!$up) {
            return array('code' => -1, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }
}